import org.springframework.web.servlet.support.RequestContextUtils as RCU;
 

class PanelTagLib {

 

    static namespace = "x"
 

    def perfectTime = {attrs, body ->
       
        int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY)
        if (hour >= 6 && hour < 8) {
            out << "早上好" 
        } else if (hour >= 8 && hour < 11) {
            out << "上午好" 
        } else if (hour >= 11 && hour < 13) {
            out << "中午好" 
        } else if (hour >= 13 && hour < 18) {
            out <<"下午好" 
        } else {
            out <<  "晚上好" 
        } 
 
    }
 

 
      /**
   * Creates next/previous links to support pagination for the current controller
   *
   * <g:paginate total="${Account.count()}" />
   */
    def paginate = {attrs ->
        def writer = out
        if (attrs.total == null)
          throwTagError("Tag [paginate] is missing required attribute [total]")
     

        def total = attrs.total.toInteger()
        def action = (attrs.action ? attrs.action : (params.action ? params.action : "list"))
        def offset = (params.offset ? params.offset.toInteger() : 0)
        def max = (params.max ? params.max.toInteger() : 10)
        def maxsteps = (attrs.maxsteps ? attrs.maxsteps.toInteger() : 10)

        if (!offset) offset = (attrs.offset ? attrs.offset.toInteger() : 0)
        if (!max) max = (attrs.max ? attrs.max.toInteger() : 10)

        def linkParams = [offset: offset - max, max: max]
        if (params.sort) linkParams.sort = params.sort
        if (params.order) linkParams.order = params.order
        if (attrs.params) linkParams.putAll(attrs.params)

        def linkTagAttrs = [action: action]
        if (attrs.controller) {
          linkTagAttrs.controller = attrs.controller
        }
        if (attrs.id != null) {
          linkTagAttrs.id = attrs.id
        }
        linkTagAttrs.params = linkParams

        // determine paging variables
        def steps = maxsteps > 0
        int currentstep = (offset / max) + 1
        int firststep = 1
        int laststep = Math.round(Math.ceil(total / max))
      
        if (currentstep > firststep) {
          linkTagAttrs.class = 'index'
          linkParams.offset = 0
          writer << link(linkTagAttrs.clone()) {
            "&nbsp;首页&nbsp;"
          } 
        } else {
          writer << "<span class='disabled'>&nbsp;首页&nbsp;</span>"
        }
    
        if(steps && laststep > firststep) {
			linkTagAttrs.class = 'pageNumber'  
			int beginstep = currentstep - Math.round(maxsteps / 2) + (maxsteps % 2)
			int endstep = currentstep + Math.round(maxsteps / 2) - 1

			if(beginstep < firststep) {
				beginstep = firststep
				endstep = maxsteps
			}
			if(endstep > laststep) {
				beginstep = laststep - maxsteps + 1
				if(beginstep < firststep) {
					beginstep = firststep
				}
				endstep = laststep
			}

			// display firststep link when beginstep is not firststep
			if(beginstep > firststep) {
				linkParams.offset = 0
				writer << link(linkTagAttrs.clone()) {firststep.toString()}
				writer << '<span class="step">..</span>'
			}

			// display paginate steps
			(beginstep..endstep).each { i ->
				if(currentstep == i) {
					writer << "<span class=\"current\">${i}</span>"
				}
				else {
					linkParams.offset = (i - 1) * max
					writer << link(linkTagAttrs.clone()) {i.toString()}
				}
			}

			// display laststep link when endstep is not laststep
			if(endstep < laststep) {
				writer << '<span class="step">..</span>'
				linkParams.offset = (laststep -1) * max
				writer << link(linkTagAttrs.clone()) { laststep.toString() }
			}
		}
        // display next link when not on laststep
        if (currentstep < laststep) {
          linkTagAttrs.class = 'last'
          linkParams.offset = (laststep - 1) * max
          writer << link(linkTagAttrs.clone()) {
            "&nbsp;尾页&nbsp;"
          }
        } else {
           writer << "<span class='disabled'>&nbsp;尾页&nbsp;</span>"

        }
    }


}
	
	